import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    redirect: "/dashboard",
    component: () =>
      import(/* webpackChunkName: "layout" */ "@/layouts/BasicLayout")
  },
  {
    path: "/login",
    name: "login",
    component: () => import(/* webpackChunkName: "user"*/ "@/views/Login")
  },
  {
    path: "/dict/catalog",
    name: "dictCatalog",
    component: () =>
      import(/* webpackChunkName: "user"*/ "@/views/dict/DictCatalogList")
  },
  {
    path: "/dashboard",
    component: () => import("@/layouts/ContentLayout"),
    children: [
      {
        path: "/dashboard",
        redirect: "/dashboard/analysis"
      },
      {
        path: "/dashboard/analysis",
        name: "/dashboard/analysis",
        component: () =>
          import(/* webpackChunkName: "user"*/ "@/views/dashboard/Analysis"),
        meta: {
          keepAlive: false // 不需要缓存
        }
      }
    ]
  },
  {
    path: "/activity",
    name: "/activity",
    component: () =>
      import(/* webpackChunkName: "layout" */ "@/components/ActitityForm"),
    meta: {
      keepAlive: true, // 不需要缓存
      name: "Activity",
      title: "活动管理"
    }
  },
  {
    path: "/hello",
    component: () =>
      import(/* webpackChunkName: "layout" */ "@/components/HelloWorld"),
    meta: {
      keepAlive: true, // 不需要缓存
      name: "HelloWorld",
      title: "测试标题"
    }
  },
  {
    path: "/user",
    name: "/user",
    redirect: "/user/",
    component: () =>
      import(/* webpackChunkName: "layout" */ "@/layouts/ContentLayout"),
    children: [
      {
        path: "staff",
        component: () =>
          import(/* webpackChunkName: "user"*/ "@/views/user/staff/StaffList")
      },

      {
        path: "/user/register",
        component: () =>
          import(/* webpackChunkName: "user"*/ "@/views/user/Register")
      }
    ]
  }
];

const router = new VueRouter({
  routes
});
// 全局路由守卫
router.beforeEach((to, from, next) => {
  console.log("路由守卫", from, to);
  if (to.path != "/login") {
    console.log("全局路由守卫---要判断是否登录");
  }
  if (to.meta.title) {
    document.title = to.meta.title;
  }
  next();
});

export default router;
